package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.platform.ai.control.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.mvparms.demo.di.component.DaggerUserComponent;
import me.jessyan.mvparms.demo.di.module.UserModule;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.entity.Device;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import me.jessyan.mvparms.demo.mvp.presenter.UserPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.LightMainAdapter;
import me.jessyan.mvparms.demo.mvp.ui.holder.ItemClickListener;


@SuppressLint("ValidFragment")
public class SceneFragment extends BaseFragment<UserPresenter> implements UserContract.View,SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.tv_title)
    TextView mTitle;

    @BindView(R.id.rv)
    RecyclerView mRv;

    LightMainAdapter mainAdapter;
    List<Device> mDevices = new ArrayList<>();
    public static SceneFragment newInstance() {
        SceneFragment fragment = new SceneFragment();
        return fragment;
    }

    public SceneFragment(){

    }

    @Override
    public void onResume() {
        super.onResume();
        if(getUserVisibleHint()){
            requestData();
        }
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .userModule(new UserModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_scene, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitle.setText("音乐");

        initRecycleView();
        requestData();
    }


    public void requestData() {
        mDevices.clear();
        mDevices.add(new Device("娱乐",R.mipmap.recreation_on,1));
        mDevices.add(new Device("休息",R.mipmap.rest_off,0));
        mDevices.add(new Device("阅读",R.mipmap.reading_off,0));
        mDevices.add(new Device("返回",R.mipmap.back_off,0));
        mainAdapter.notifyDataSetChanged();

    }

    private void initRecycleView() {
        mainAdapter = new LightMainAdapter(mDevices, new ItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                switch (position){
                    case 0:
                        mDevices.get(0).setIcon(R.mipmap.recreation_on);
                        mDevices.get(0).setState(1);
                        mDevices.get(1).setIcon(R.mipmap.rest_off);
                        mDevices.get(1).setState(0);
                        mDevices.get(2).setIcon(R.mipmap.reading_off);
                        mDevices.get(2).setState(0);
                        mDevices.get(3).setIcon(R.mipmap.back_off);
                        mDevices.get(3).setState(0);
                        break;
                    case 1:
                        mDevices.get(0).setIcon(R.mipmap.recreation_off);
                        mDevices.get(0).setState(0);
                        mDevices.get(1).setIcon(R.mipmap.rest_on);
                        mDevices.get(1).setState(1);
                        mDevices.get(2).setIcon(R.mipmap.reading_off);
                        mDevices.get(2).setState(0);
                        mDevices.get(3).setIcon(R.mipmap.back_off);
                        mDevices.get(3).setState(0);
                        break;
                    case 2:
                        mDevices.get(0).setIcon(R.mipmap.recreation_off);
                        mDevices.get(0).setState(0);
                        mDevices.get(1).setIcon(R.mipmap.rest_off);
                        mDevices.get(1).setState(0);
                        mDevices.get(2).setIcon(R.mipmap.reading_on);
                        mDevices.get(2).setState(1);
                        mDevices.get(3).setIcon(R.mipmap.back_off);
                        mDevices.get(3).setState(0);
                        break;
                    case 3:
                        mDevices.get(0).setIcon(R.mipmap.recreation_off);
                        mDevices.get(0).setState(0);
                        mDevices.get(1).setIcon(R.mipmap.rest_off);
                        mDevices.get(1).setState(0);
                        mDevices.get(2).setIcon(R.mipmap.reading_off);
                        mDevices.get(2).setState(0);
                        mDevices.get(3).setIcon(R.mipmap.back_on);
                        mDevices.get(3).setState(1);
                        break;
                }
                mainAdapter.notifyDataSetChanged();
            }
        });
        mRv.setAdapter(mainAdapter);
        ArmsUtils.configRecycleView(mRv, new GridLayoutManager(getContext(),2));


    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }


    @Override
    public void showMessage(@NonNull String message) {
        if (!message.isEmpty()) {

        }
    }


    @Override
    public void setAdapter(DefaultAdapter adapter) {

    }

    @Override
    public void loginReturn(User user) {

    }

    @Override
    public void logout() {

    }

    @Override
    public void uploadResult(String string) {

    }


    @Override
    public void onRefresh() {
        requestData();
    }
}
